package com.fc.controller;


import com.fc.anno.Log;
import com.fc.dto.RegionDTO;
import com.fc.dto.pageQuery.RegionPageQueryDTO;
import com.fc.entity.Region;
import com.fc.result.PageResult;
import com.fc.result.Result;
import com.fc.service.IRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "区域")
@RestController
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private IRegionService regionService;


    /**
     * 添加区域
     *
     * @param regionDTO
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("添加区域")
    @PostMapping
    public Result save(@RequestBody RegionDTO regionDTO) {
        regionService.saveRegion(regionDTO);
        return Result.success();
    }


    /**
     * 删除区域
     *
     * @param id
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("删除区域")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        regionService.deleteRegion(id);
        return Result.success();
    }


    /**
     * 更新区域
     *
     * @param regionDTO
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("更新区域")
    @PutMapping
    public Result update(@RequestBody RegionDTO regionDTO) {
        regionService.updateRegion(regionDTO);
        return Result.success();
    }

    /**
     * 查询区域列表
     *
     * @return
     */
    @ApiOperation("查询区域列表")
    @GetMapping("/list")
    public Result<List<Region>> list() {
        List<Region> regionList = regionService.list();
        return Result.success(regionList);
    }

    /**
     * 条件查询
     *
     * @return
     */
    @ApiOperation("条件查询")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody RegionPageQueryDTO regionPageQueryDTO) {
        PageResult pageResult = regionService.pageRegion(regionPageQueryDTO);
        return Result.success(pageResult);
    }

}
